<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\cardeadmin\controller;

use app\common\controller\AdminController;
use app\common\model\ManageRole;
use app\common\model\Manage as ManageModel;
use app\common\model\ManageRoleRel;
use think\facade\Db;
use think\facade\View;


/**
 * 管理员
 * Class Administrator
 * @package app\admin\controller
 */
class Administrator extends AdminController
{
    /**
     * 管理员列表
     * @author 救火队队长
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $manageModel = new ManageModel();
            return $manageModel->tableData(input('param.'));
        }
        return View::fetch();
    }


    /**
     * 添加管理员
     * @author 救火队队长
     */
    public function add()
    {
        $result = [
            'status' => false,
            'msg'    => '失败',
            'datas'   => ''
        ];

        View::layout(false);
        $manageModel     = new ManageModel();
        $manageRoleModel = new ManageRole();
        $manageRoleList  = $manageRoleModel->select();
        if ($this->request->isPost()) {
            if (!input('?param.username') || input('param.username') == "") {
                return error_code(11008);
            }
            if (!input('?param.mobile') || input('param.mobile') == "") {
                return error_code(11080);
            }
            if (!input('?param.password') || strlen(input('param.password')) < 6 || strlen(input('param.password')) > 16) {
                return error_code(11009);
            }
            return $manageModel->toAdd(input('param.'));
        }

        View::assign('roleList', $manageRoleList);
        View::assign('manageInfo', []);
        $result['status'] = 0;
        $result['msg']    = '成功';
        $result['datas']   = View::fetch('edit');
        return $result;
    }


    /**
     * 编辑管理员
     * @author 救火队队长
     */
    public function edit()
    {
        $result = [
            'status' => false,
            'msg'    => '失败',
            'data'   => ''
        ];

        View::layout(false);
        if (!input('?param.id')) {
            return error_code(10000);
        }

        $manageModel = new ManageModel();
        if (input('param.id') == $manageModel::TYPE_SUPER_ID) {
            return error_code(11023);
        }
        $manageInfo = $manageModel->where(['id' => input('param.id')])->find();
        if (!$manageInfo) {
            return error_code(11004);
        }

        if ($this->request->isPost()) {
            return $manageModel->toAdd(input('param.'));
        }

        $manageRoleModel    = new ManageRole();
        $manageRoleList     = $manageRoleModel->select();
        $manageRoleRelModel = new ManageRoleRel();
        $smList             = $manageRoleRelModel->where(['manage_id' => input('param.id')])->select();
        foreach ($manageRoleList as $k => $v) {
            $checked = false;
            foreach ($smList as $i => $j) {
                if ($j['role_id'] == $v['id']) {
                    $checked = true;
                    break;
                }
            }
            $manageRoleList[$k]['checked'] = $checked;
        }
        View::assign('roleList', $manageRoleList);
        View::assign('manageInfo', $manageInfo);
        $result['status'] = 0;
        $result['msg']    = '成功';
        $result['datas']   = View::fetch('edit');
        return $result;
    }


    /**
     * 删除管理员
     * @author 救火队队长
     */
    public function del()
    {
        $result = [
            'status' => false,
            'msg'    => '失败',
            'data'   => ''
        ];
        if (!input('?param.id')) {
            return error_code(10000);
        }

        $manageModel = new manageModel();
        if (input('param.id') == $manageModel::TYPE_SUPER_ID) {
            return error_code(11024);
        }

        $where['id'] = input('param.id');
        $re          = $manageModel->where($where)->delete();
        if ($re) {
            $result['status'] = true;
            $result['msg']    = '删除成功';
        } else {
            $result['msg'] = '删除失败，请重试';
        }

        return $result;
    }


    /**
     * 获取用户信息
     * @author 救火队队长
     */
    public function information()
    {
        $result      = [
            'status' => false,
            'msg'    => '失败',
            'data'   => ''
        ];
        $manageModel = new ManageModel();
        $manageInfo  = $manageModel->where(['id' => session('manage.id')])->find();
        View::assign('manage_info', $manageInfo);
        return View::fetch();
    }


    /**
     * 用户修改/找回密码
     * @author 救火队队长
     * @return array|string
     */
    public function editPwd()
    {
        $result      = [
            'status' => false,
            'msg'    => '失败',
            'data'   => ''
        ];
        $manageModel = new ManageModel();

        if (!input('?param.newPwd') || !input('?param.password') || !input('?param.rePwd')) {
            $result['msg'] = "密码不能为空";
            return $result;
        }
        if (input('param.newPwd') != input('param.rePwd')) {
            $result['msg'] = "两次密码不一致";
            return $result;
        }

        return $manageModel->chengePwd(session('manage.id'), input('param.password'), input('param.newPwd'));
    }

    /**
     * 编辑余额
     * @author 救火队队长
     */
    public function editMoney()
    {
        if($this->request->isPost()) {
            $data       = input('param.');
            $money      = $data['money'];
            $manage_id    = $data['manage_id'];
            if(!check_param($money , 'zfInt')) return $this->err("It must be a positive integer.");
            if($money == 0) return $this->err("Amount cannot be equal to 0.");
            $manage_list = Db::name('manage')->where(['id' => $manage_id])->find();
            $current_balance = abs($manage_list['balance']);
            $user_name       = $manage_list['username'];
            if($money < 0){
                if(($current_balance - abs($money)) <0){
                    return $this->err("Insufficient account balance.");
                }
            }
            $data += ['manage_id_login' => $this->manage_id ,
                'balance' => $current_balance ,
                'username'=> $user_name ];
            $manageMode = new ManageModel();
            $re = $manageMode->changeBalance($data);
            if($re){
                return $this->scs('Confirm the successful.');
            }else{
                return $this->scs('Confirm the failed.');
            }
        }

        $manage_id = input('get.manage_id');
        $manage_info = Db::name('manage')->where(['id' => $manage_id])->find();
        View::assign([
            'manage_id'   => $manage_id,
            'money'     => $manage_info['balance']
        ]);
        View::layout(false);
        return [
            'status'    => 1,
            'datas'     => View::fetch()
        ];
    }
}